import { getListApi } from "@/api/user";
import { ListUserParm } from "@/api/user/UserModel";
import { onMounted, reactive,ref,nextTick } from "vue";

//用户管理有关的表格相关的业务
export default function useUserTable(){
    //表格高度
    const tableHeight = ref(0)
    //列表查询的参数
    const listParm = reactive<ListUserParm>({//限制传递参数的类型为ListUserParm
        page:1,
        pageSize:10,
        totalSize:0,
        name:'',
        phone:'',
        username:'',
    })
    //表格的数据
    const tableList = ref([])
    //列表
    const getList = async()=>{
        let result = await getListApi(listParm)
        if(result && result.code==200){
            listParm.totalSize = result.data.totalPage;
            tableList.value = result.data.data;
        }
    }
    //搜索
    const searchBtn = ()=>{
        getList()
    }
    //重置
    const resetBtn = ()=>{
        listParm.page = 1;
        listParm.name = ''
        listParm.phone = ''
        getList()
    }
    //总页数改变时触发
    const sizeChange = (size:number)=>{
        listParm.pageSize = size;
        getList()
    }
    //当前页数改变时触发
    const currentChange = (page:number)=>{
        listParm.page = page;
        getList()
    }
    onMounted(()=>{
        getList()
        //计算表格高度
        nextTick(()=>{
            tableHeight.value = window.innerHeight -435
        })
    })
    return{
        listParm,
        getList,
        searchBtn,
        resetBtn,
        tableList,
        sizeChange,
        currentChange,
        tableHeight,
    }
}